﻿Public Class GeolGeomSit

    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Dim TextBoxID As String
    Dim Niveau As Integer = 1
    Dim LibelleNiv As String()
    Dim CodeNiv As String()
    Dim NomNiv As String()
    Dim IdNiv As String()
    Dim Nomtable As String()
    Dim NbrNiv As Integer = 0
    Dim idAct As String = ""
    Public databaseName As String
    Dim idAct1 As String = ""
    Dim NbrNiv1 As Integer = 0
    Dim Niveau1 As Integer = 1
    Dim LibelleNiv1 As String()
    Dim CodeNiv1 As String()
    Dim NomNiv1 As String()
    Dim IdNiv1 As String()
    Dim Nomtable1 As String()

    Private Sub GeolGeomSit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        preNiv1()
        preNiv2()
        majTypRoch()
        majTypGeol()
        miseajour()
    End Sub

    Private Sub majTypGeol()

        Dim requeteX As String
        Dim niv As Integer = 1


        requeteX = "Select * from  [dbo].[StrGeolTyp] WHERE " & Nomtable1(3 + NbrNiv) & " = '" & Niveau1 & "'"

        While niv < Niveau1
            requeteX = requeteX & "and " & Nomtable1(2 + niv) & " = '" & CodeNiv1(niv - 1) & "' "
            niv = niv + 1
        End While


        If Niveau1 = 1 Then
            Me.ButtonBack1.Enabled = False
            Me.ButtonNext1.Enabled = True
        ElseIf Niveau1 = NbrNiv1 Then
            Me.ButtonBack1.Enabled = True
            Me.ButtonNext1.Enabled = False
        Else
            Me.ButtonBack1.Enabled = True
            Me.ButtonNext1.Enabled = True
        End If

        db.myReq = requeteX
        db.Connecter(databaseName)

        Me.ComboBoxCodeTyp1.Items.Clear()
        Me.ComboBoxNomType1.Items.Clear()
        Me.ComboBoxNomType1.Text = ""
        While db.rdr.Read()
            Me.ComboBoxNomType1.Items.Add(db.rdr(NbrNiv1 + 4))
            Me.ComboBoxCodeTyp1.Items.Add(db.rdr(0))
        End While
        db.Disconnecter()

        If ComboBoxCodeTyp1.Items.Count < 0 Or Niveau1 = NbrNiv1 Then
            Me.ButtonNext1.Enabled = False
        Else
            Me.ButtonNext1.Enabled = True
        End If

    End Sub
    Private Sub ComboBoxCodeTyp1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomType1.TextChanged

        ' If Me.ComboBoxNomType1.FindString(Me.ComboBoxNomType1.Text) > 0 Then
        Dim Pos As Int32 = Me.ComboBoxNomType1.Text.Length
        Me.ComboBoxNomType1.SelectedIndex = Me.ComboBoxNomType1.FindString(Me.ComboBoxNomType1.Text)
        Me.ComboBoxNomType1.SelectionStart = Pos
        Me.ComboBoxNomType1.SelectionLength = Me.ComboBoxNomType1.Text.Length - Pos
        Me.ComboBoxCodeTyp1.SelectedIndex = Me.ComboBoxNomType1.FindString(Me.ComboBoxNomType1.Text)
        ' Else
        ' Me.ComboBoxCodeTyp.Text = ""
        ' End If
    End Sub

    Private Sub majTypRoch()
        Dim requeteX As String
        Dim niv As Integer = 1


        requeteX = "Select * from  [dbo].[RochesTyp] WHERE " & Nomtable(3 + NbrNiv) & " = '" & Niveau & "'"

        While niv < Niveau
            requeteX = requeteX & " and " & Nomtable(2 + niv) & " = '" & CodeNiv(niv - 1) & "' "
            niv = niv + 1
        End While


        If Niveau = 1 Then
            Me.ButtonBack.Enabled = False
            Me.ButtonNext.Enabled = True
        ElseIf Niveau = NbrNiv Then
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonBack.Enabled = True
            Me.ButtonNext.Enabled = True
        End If

        db.myReq = requeteX
        db.Connecter(databaseName)

        Me.ComboBoxCodeTyp.Items.Clear()
        Me.ComboBoxNomType.Items.Clear()
        Me.ComboBoxNomType.Text = ""
        While db.rdr.Read()
            Me.ComboBoxNomType.Items.Add(db.rdr(NbrNiv + 4))
            Me.ComboBoxCodeTyp.Items.Add(db.rdr(0))
        End While
        db.Disconnecter()

        If ComboBoxCodeTyp.Items.Count < 0 Or Niveau = NbrNiv Then
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonNext.Enabled = True
        End If

    End Sub
    Private Sub ComboBoxCodeTyp_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomType.TextChanged

        'If Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text) > 0 Then
        Dim Pos As Int32 = Me.ComboBoxNomType.Text.Length
        Me.ComboBoxNomType.SelectedIndex = Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text)
        Me.ComboBoxNomType.SelectionStart = Pos
        Me.ComboBoxNomType.SelectionLength = Me.ComboBoxNomType.Text.Length - Pos
        Me.ComboBoxCodeTyp.SelectedIndex = Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text)
        'Else
        'Me.ComboBoxCodeTyp.Text = ""
        'End If
    End Sub

    Private Sub miseajour()

    End Sub

    Private Sub preNiv1()
        db.myReq = "SELECT COUNT(syscolumns.name) AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'RochesTyp'"
        db.Connecter(databaseName)
        While db.rdr.Read()
            NbrNiv = db.rdr(0) - 6
        End While
        db.Disconnecter()
        LibelleNiv = New String(NbrNiv) {}
        CodeNiv = New String(NbrNiv) {}
        NomNiv = New String(NbrNiv) {}
        IdNiv = New String(NbrNiv) {}
        Nomtable = New String(NbrNiv + 6) {}
        Dim iter As Integer = 1
        While iter < NbrNiv
            db.myReq = "SELECT [Libelle_Niveau] FROM  [dbo].[Niveau] Where [Table_Niveau]='RochesTyp' and [Niv_Niveau]='" & iter & "'"
            db.Connecter(databaseName)
            While db.rdr.Read()
                LibelleNiv(iter - 1) = db.rdr(0)
            End While
            db.Disconnecter()
            iter = iter + 1
        End While

        db.myReq = "SELECT syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'RochesTyp'"
        db.Connecter(databaseName)
        iter = 0
        While db.rdr.Read()
            Nomtable(iter) = db.rdr(0)
            iter = iter + 1
        End While
        db.Disconnecter()
    End Sub

    Private Sub preNiv2()
        db.myReq = "SELECT COUNT(syscolumns.name) AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'StrGeolTyp'"
        db.Connecter(databaseName)
        While db.rdr.Read()
            NbrNiv1 = db.rdr(0) - 6
        End While
        db.Disconnecter()
        LibelleNiv1 = New String(NbrNiv1 - 1) {}
        CodeNiv1 = New String(NbrNiv1 - 1) {}
        NomNiv1 = New String(NbrNiv1 - 1) {}
        IdNiv1 = New String(NbrNiv1 - 1) {}
        Nomtable1 = New String(NbrNiv1 + 6) {}
        Dim iter As Integer = 1
        While iter < NbrNiv1
            db.myReq = "SELECT [Libelle_Niveau] FROM  [dbo].[Niveau] Where [Table_Niveau]='StrGeolTyp' and [Niv_Niveau]='" & iter & "'"
            db.Connecter(databaseName)
            While db.rdr.Read()
                LibelleNiv1(iter - 1) = db.rdr(0)
            End While
            db.Disconnecter()
            iter = iter + 1
        End While

        db.myReq = "SELECT syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'StrGeolTyp'"
        db.Connecter(databaseName)
        iter = 0
        While db.rdr.Read()
            Nomtable1(iter) = db.rdr(0)
            iter = iter + 1
        End While
        db.Disconnecter()
    End Sub



    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Try
            Dim idMax As Integer
            db.myReq = "INSERT INTO  [dbo].[GEOLGEOMSIT]([ID_Sit],[CARROC_GEOGL],[CARSTRU_GEOGL],[EXPLROC_GEOGL]) VALUES('" & db.idSite & "','" & Me.TextBoxCarRoch.Text & "','" & Me.TextBoxCarStr.Text & "','" & Me.TextBoxDesGlob.Text & "')"
            db.Connecter(databaseName)
            db.Disconnecter()
            db.myReq = "SELECT Max([Id_GeoGl]) FROM  [dbo].[GEOLGEOMSIT]"
            db.Connecter(databaseName)
            While db.rdr.Read
                idMax = db.rdr(0)
            End While
            db.Disconnecter()
            db.myReq = "INSERT INTO  [dbo].[GeolRoches]([Id_GeoGl],[Id_RochesTyp],[Code_GeolRoch],[Loc_GeolRoch],[Expl_GeolRoch]) VALUES('" & idMax & "','" & Me.ComboBoxCodeTyp.Text & "','','" & Me.TextBoxLoc.Text & "','" & Me.TextBoxDesc.Text & "')"
            db.Connecter(databaseName)
            db.Disconnecter()
            db.myReq = "INSERT INTO  [dbo].[GeolStr]([Id_StrG],[Id_GeoGl],[Code_GeolStr],[Loc_GeolStr],[Expl_GeolStr]) VALUES('" & Me.ComboBoxCodeTyp1.Text & "','" & idMax & "','','" & Me.TextBoxLoc1.Text & "','" & Me.TextBoxDesc1.Text & "')"
            db.Connecter(databaseName)
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
        miseajour()
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Dim typX As New TypologieNivXssCatg
        typX.Text = "Typologie"
        typX.Table2 = "RochesTyp"
        typX.Visible = True

    End Sub

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Dim typX As New TypologieNivXssCatg
        typX.Text = "Typologie"
        typX.Table2 = "StrGeolTyp"
        typX.Visible = True
    End Sub

    Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
        Entrer = ""
        Niveau = Niveau + 1
        Me.LabelNiv.Text = LibelleNiv(Niveau)

        db.myReq = "Select * from  [dbo].[RochesTyp] WHERE " & Nomtable(0) & " = '" & Me.ComboBoxCodeTyp.Text & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            CodeNiv(Niveau - 2) = db.rdr(1 + Niveau)
        End While

        majTypRoch()
    End Sub
    Private Sub ButtonBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBack.Click
        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab.Text = ""
        Entrer = ""
        Niveau = Niveau - 1
        Me.LabelNiv.Text = LibelleNiv(Niveau - 1)
        While niv < Niveau
            Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
            niv = niv + 1
        End While
        majTypRoch()
        ' Me.ComboBoxNomType.SelectedIndex = Me.ComboBoxNomType.Items.IndexOf(IdNiv(Niveau - 1))
    End Sub


    Private Sub ButtonNext1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext1.Click
        Entrer = ""
        Niveau1 = Niveau1 + 1
        Me.LabelNiv1.Text = LibelleNiv1(Niveau1)

        db.myReq = "Select * from  [dbo].[StrGeolTyp] WHERE " & Nomtable1(0) & " = '" & Me.ComboBoxCodeTyp1.Text & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            CodeNiv1(Niveau1 - 2) = db.rdr(1 + Niveau1)
        End While

        majTypGeol()
    End Sub
    Private Sub ButtonBack1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBack1.Click
        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab1.Text = ""
        Entrer = ""
        Niveau1 = Niveau1 - 1
        Me.LabelNiv1.Text = LibelleNiv1(Niveau1 - 1)
        While niv < Niveau1
            Me.TextBoxDecoupageHab1.Text = Me.TextBoxDecoupageHab1.Text & NomNiv1(niv)
            niv = niv + 1
        End While
        majTypGeol()
        Me.ComboBoxNomType1.SelectedIndex = Me.ComboBoxNomType1.Items.IndexOf(IdNiv1(Niveau1 - 1))
    End Sub


End Class
